<?php
/*
 * Created on 2012-5-9
 * @iChying
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */

 include_once($_SERVER["DOCUMENT_ROOT"].'/URL/FileOperator.php');
 include_once($_SERVER["DOCUMENT_ROOT"].'/URL/Charset/PHPConvert.php');

 class ChineseChar {
 	public $fileOperatorHandler;
 	public $charsetConvertHandler;

 	public $minGB2312Unicode;
 	public $maxGB2312Unicode;

 	public $minBIG5Unicode;
 	public $maxBIG5Unicode;

 	public $minGBKUnicode;
 	public $maxGBKUnicode;


 	public function __construct() {
		$this->fileOperatorHandler = new FileOperator();
		$this->charsetConvertHandler = new PHPCharsetConvert();
 	}

 	public function genGB2312Char() {
 		$gb1232Char = '';
 		$this->minGB2312Unicode = 0xffff;
 		$this->maxGB2312Unicode = 0x0000;

 		$chineseChar = '';
		$chineseInfo = 'name'.' gbCP'.'    Unicode'."\r\n";

		$mode = 0;
		$writeMode = array('w', 'a');

		$count = 0;


 		for ($highByte = 0xb0; $highByte <= 0xf7; ++$highByte) {
 			for ($lowByte = 0xa1; $lowByte <= 0xfe; ++$lowByte) {
 				if ($highByte == 0xd7 && ($lowByte >= 0xfa && $lowByte <= 0xd7fe)) {
 					continue;
 				} else {
 					$gb2312Code = dechex($highByte).dechex($lowByte);
 					$chEntity = chr($highByte).chr($lowByte);
 					$chUnicode = $this->charsetConvertHandler->GBK2Unicode($chEntity);

 					$this->minGB2312Unicode = hexdec($chUnicode) < $this->minGB2312Unicode ? hexdec($chUnicode) : $this->minGB2312Unicode;
 					$this->maxGB2312Unicode = hexdec($chUnicode) > $this->maxGB2312Unicode ? hexdec($chUnicode) : $this->maxGB2312Unicode;

 					$chineseChar .= $chEntity;
 					$chineseInfo .= $chEntity.'    '.$gb2312Code.'    '.$chUnicode."\r\n";

 					if ((++$count) == 2000) {
 						$this->fileOperatorHandler->write_file($_SERVER["DOCUMENT_ROOT"].'/URL/data/gb2312.txt', $chineseChar, $writeMode[$mode]);
 						$this->fileOperatorHandler->write_file($_SERVER["DOCUMENT_ROOT"].'/URL/data/gb2312info.txt', $chineseInfo, $writeMode[$mode]);
 						$chineseChar = '';
 						$chineseInfo = '';

 						$count = 0;

 						if ($mode == 0) $mode = 1;
 					}
 				}
 			}
 		}

 		$this->fileOperatorHandler->write_file($_SERVER["DOCUMENT_ROOT"].'/URL/data/gb2312.txt', $chineseChar, $writeMode[$mode]);
 		$this->fileOperatorHandler->write_file($_SERVER["DOCUMENT_ROOT"].'/URL/data/gb2312info.txt', $chineseInfo, $writeMode[$mode]);

 		echo 'generate gb2312 character finish!<br />';

		//echo 'min = ',dechex($this->minGB2312Unicode), '<br />';
 		//echo 'max = ',dechex($this->maxGB2312Unicode), '<br />';
 	}

 	private function genBig5($hBS, $hBE, $lBS, $lBE, &$chineseChar, &$chineseInfo, &$count, &$mode) {
 		for ($highByte = $hBS; $highByte <= $hBE; ++$highByte) {
 			for ($lowByte = $lBS; $lowByte <= $lBE; ++$lowByte) {
				$big5Code = dechex($highByte).dechex($lowByte);
 				$big5Entity = chr($highByte).chr($lowByte);
 				$chUnicode = $this->charsetConvertHandler->BIG52Unicode($big5Entity);

 				if (empty($chUnicode)) continue;

				/*
 				if (hexdec($chUnicode) >= 0xf800) {
 					echo $big5Entity, ' ', $chUnicode, ' ', $big5Code, '<br />';
 					// continue;
 				}*/

 				$this->minBIG5Unicode = hexdec($chUnicode) < $this->minBIG5Unicode ? hexdec($chUnicode) : $this->minBIG5Unicode;
 				$this->maxBIG5Unicode = hexdec($chUnicode) > $this->maxBIG5Unicode ? hexdec($chUnicode) : $this->maxBIG5Unicode;

				$chineseChar .= $this->charsetConvertHandler->Unicode2GBKentity(hexdec($chUnicode));
				$chineseInfo .= $this->charsetConvertHandler->Unicode2GBKentity(hexdec($chUnicode)).'   '.$big5Code.'    '.$chUnicode."\r\n";

				if ((++$count) == 2000) {
					//echo $chineseInfo, '<br />';

					$this->fileOperatorHandler->write_file($_SERVER["DOCUMENT_ROOT"].'/URL/data/big5.txt', $chineseChar, $mode);
 					$this->fileOperatorHandler->write_file($_SERVER["DOCUMENT_ROOT"].'/URL/data/big5info.txt', $chineseInfo, $mode);
 					$chineseChar = '';
 					$chineseInfo = '';

 					$count = 0;
 					if ($mode == 'w') $mode = 'a';
				}
 			}
 		}
 	}

 	public function genBIG5Char() {
 		$chineseChar = '';
		$chineseInfo = 'name'.' big5CP'.' Unicode'."\r\n";

		$count = 0;
		$mode = 'w';

 		$this->minBIG5Unicode = 0xffff;
 		$this->maxBIG5Unicode = 0x0000;

 		// 0xA259-0xA261
 		$this->genBig5(0xa2, 0xa2, 0x59, 0x61, $chineseChar, $chineseInfo, $count, $mode);

 		// 0xA440-0XC67E
 		$this->genBig5(0xa4, 0xc6, 0x40, 0x7e, $chineseChar, $chineseInfo, $count, $mode);

 		// 0xC940-0xF9D5
 		$this->genBig5(0xc9, 0xf9, 0x40, 0xd5, $chineseChar, $chineseInfo, $count, $mode);

 		// 0xF9D6-0xF9FE(Code Page 950)
 		$this->genBig5(0xf9, 0xf9, 0xd6, 0xfe, $chineseChar, $chineseInfo, $count, $mode);

 		$this->fileOperatorHandler->write_file($_SERVER["DOCUMENT_ROOT"].'/URL/data/big5.txt', $chineseChar, $mode);
 		$this->fileOperatorHandler->write_file($_SERVER["DOCUMENT_ROOT"].'/URL/data/big5info.txt', $chineseInfo, $mode);

 		//echo 'big5 min unicode = ',dechex($this->minBIG5Unicode), '<br />';
 		//echo 'big5 max unicode = ',dechex($this->maxBIG5Unicode), '<br />';
 		echo 'generate big5 character finish!<br />';
 	}

	private function genGBK($hBS, $hBE, $lBS, $lBE, &$gbkChar, &$gbkInfo, &$count, &$mode) {
		for ($highByte = $hBS; $highByte <= $hBE; ++$highByte) {
			for ($lowByte = $lBS; $lowByte <= $lBE; ++$lowByte) {
				if ($lowByte == 0x7f) continue;

				$gbkCode = dechex($highByte).dechex($lowByte);
				$gbkEntity = chr($highByte).chr($lowByte);
				$gbkUnicode = $this->charsetConvertHandler->GBK2Unicode($gbkEntity);

				if (empty($gbkUnicode)) continue;

				if (hexdec($gbkUnicode) >= 0xf800) {
					//echo $gbkEntity,'    ',$gbkCode,'    ',$gbkUnicode, ' ', decbin(hexdec($gbkUnicode)), '<br />';
				}

				$this->minGBKUnicode = hexdec($gbkUnicode) < $this->minGBKUnicode ? hexdec($gbkUnicode) : $this->minGBKUnicode;
 				$this->maxGBKUnicode = hexdec($gbkUnicode) > $this->maxGBKUnicode ? hexdec($gbkUnicode) : $this->maxGBKUnicode;

 				$gbkChar .= $gbkEntity;
 				$gbkInfo .= $gbkEntity.'    '.$gbkCode.'    '.$gbkUnicode."\r\n";

 				if ((++$count) == 1000) {
					$this->fileOperatorHandler->write_file($_SERVER["DOCUMENT_ROOT"].'/URL/data/gbk.txt', $gbkChar, $mode);
 					$this->fileOperatorHandler->write_file($_SERVER["DOCUMENT_ROOT"].'/URL/data/gbkInfo.txt', $gbkInfo, $mode);
 					$gbkChar = '';
 					$gbkInfo = '';

 					$count = 0;
 					if ($mode == 'w') $mode = 'a';
				}
			}
		}
	}

	// GBK and window codepage 936
 	public function genGBKChar() {
 		$gbkChar = '';
		$gbkInfo = 'name'.' gbkCP'.' Unicode'."\r\n";

		$count = 0;
		$mode = 'w';

 		$this->minGBKUnicode = 0xffff;
 		$this->maxGBKUnicode = 0x0000;

 		// 0xB0A1~0XF7FE GBK/2 6768/6763
 		$this->genGBK(0xb0, 0xf7, 0xa1, 0xfe, $gbkChar, $gbkInfo, $count, $mode);

 		// 0x8140-0xA07E 0x8180-0xA0FE GBK/3 6080/6080
		$this->genGBK(0x81, 0xa0, 0x40, 0xfe, $gbkChar, $gbkInfo, $count, $mode);

 		// 0xAA40-0xFE7E 0xAA80-0xFEA0 GBK/4 8160/8160
 		$this->genGBK(0xaa, 0xfe, 0x40, 0xa0, $gbkChar, $gbkInfo, $count, $mode);

		$this->fileOperatorHandler->write_file($_SERVER["DOCUMENT_ROOT"].'/URL/data/gbk.txt', $gbkChar, $mode);
 		$this->fileOperatorHandler->write_file($_SERVER["DOCUMENT_ROOT"].'/URL/data/gbkinfo.txt', $gbkInfo, $mode);

		//echo 'gbk min unicode = ',dechex($this->minGBKUnicode), '<br />';
 		echo 'gbk max unicode = ',dechex($this->maxGBKUnicode), '<br />';
 		echo 'generate gbk character finish!<br />';
 	}
 }
?>

<?php
 function testClass() {
 	$test = new ChineseChar();

	// generate GB2312 character
  	//$test->genGB2312Char();

	// generate BIG5 character
 	//$test->genBIG5Char();

 	// generate BIG5 character
 	$test->genGBKChar();
 }
 testClass();
?>
